<template>
  <el-row>
    <el-col :span="24">
      <el-button v-if="groupManager_menu" type="primary" @click="update">保存</el-button>
    </el-col>
    <el-col :span="6" style="margin-top:15px;">
      <el-input v-model="filterText" placeholder="输入关键字进行过滤"/>
      <el-tree
        ref="menuTree"
        :data="treeData"
        :props="defaultProps"
        :filter-node-method="filterNode"
        node-key="id"
        class="filter-tree"
        check-strictly
        show-checkbox
        highlight-current
        default-expand-all
        @node-click="getNodeData"/>
    </el-col>
    <el-col :span="16" style="margin-top:15px; margin-left:10px;">
      <el-table ref="elementTable" :data="list" border fit highlight-current-row style="width: 100%" @select="handleSelectionChange">
        <el-table-column v-if="groupManager_element" type="selection" min-width="5%" align="center"/>
        <el-table-column min-width="15%" align="center" label="资源编码"><template slot-scope="scope">
          <span>
            {{ scope.row.code }}</span>
        </template>
        </el-table-column>
        <el-table-column min-width="15%" align="center" label="资源类型"><template slot-scope="scope">
          <span>
            {{ scope.row.type }}</span>
        </template>

        </el-table-column>
        <el-table-column min-width="20%" align="center" label="资源名称"><template slot-scope="scope">
          <span>
            {{ scope.row.name }}</span>
        </template>

        </el-table-column>
        <el-table-column min-width="20%" align="center" label="资源地址"><template slot-scope="scope">
          <span>
            {{ scope.row.uri }}</span>
        </template>
        </el-table-column>
        <el-table-column min-width="10%" align="center" label="请求类型"><template slot-scope="scope">
          <span>
            {{ scope.row.method }}</span>
        </template>
        </el-table-column>
      </el-table>
    </el-col>

  </el-row>

</template>
<script>
import {
  fetchTree
} from '@/api/admin/menu/index'
import {
  page
} from '@/api/admin/element/index'
import {
  removeElementAuthority,
  addElementAuthority,
  getElementAuthority,
  modifyMenuAuthority,
  getMenuAuthority
} from '@/api/admin/group/index'
import { mapGetters } from 'vuex'
export default {
  name: 'Menu',
  props: {
    groupId: {
      default: 1,
      type: Number
    }
  },
  data() {
    return {
      filterText: '',
      list: null,
      total: null,
      listQuery: {
        name: undefined
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      groupManager_menu: false,
      groupManager_element: false
      //   currentId: -1
    }
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  watch: {
    filterText(val) {
      this.$refs.menuTree.filter(val)
    }
  },
  created() {
    this.getList()
    this.groupManager_menu = this.elements['groupManager:menu']
    this.groupManager_element = this.elements['groupManager:element']
  },
  methods: {
    getList() {
      console.log('-----------getLIst groupauthority')
      fetchTree(this.listQuery).then(data => {
        console.log('fetchTree', data)
        this.treeData = data.data
        console.log('fetchTree2', this.treeData)
        this.initAuthoritys()
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getNodeData(data) {
      console.log('getNodeData enter', data)
      this.listQuery.menuId = data.id
      page(this.listQuery).then(response => {
        console.log('getNodeData:', response)
        this.list = response.data
        console.log('getNodeData:this.list:', this.list)
        getElementAuthority(this.groupId).then(data => {
          console.log('getElementAuthority:', data)
          const obj = {}
          for (let i = 0; i < this.list.length; i++) {
            obj[this.list[i].id] = this.list[i]
          }
          const toggle = {}
          for (let i = 0; i < data.data.length; i++) {
            const id = data.data[i]
            if (obj[id] !== undefined && toggle[id] === undefined) {
              this.$refs.elementTable.toggleRowSelection(obj[data.data[i]])
              toggle[id] = true
            }
          }
        })
      })
      this.currentId = data.id
      this.showElement = true
    },
    getTreeNodeKey(node) {
      return node.id
    },
    handleSelectionChange(val, row) {
      let flag = true
      for (let i = 0; i < val.length; i++) {
        if (val[i].id === row.id) {
          addElementAuthority(this.groupId, {
            menuId: this.currentId,
            elementId: row.id
          })
          flag = false
          break
        }
      }
      if (flag) {
        removeElementAuthority(this.groupId, {
          menuId: this.currentId,
          elementId: row.id
        })
      }
    },
    update() {
      this.$emit('closeAuthorityDialog')
      const nodes = this.$refs.menuTree.getCheckedNodes()
      const ids = []
      for (let i = 0; i < nodes.length; i++) {
        ids.push(nodes[i].id)
      }
      modifyMenuAuthority(this.groupId, {
        menuTrees: ids.join()
      }).then(() => {
        this.$notify({
          title: '成功',
          message: '保存成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    initAuthoritys() {
      getMenuAuthority(this.groupId).then(data => {
        console.log('getMenuAuthority', data)
        const result = []
        for (let i = 0; i < data.data.length; i++) {
          result.push(data.data[i].id)
        }
        this.$refs.menuTree.setCheckedKeys(result)
      })
    }
  }
}
</script>
